package csel.model;

import java.util.Iterator;
import java.util.concurrent.ConcurrentLinkedQueue;

public class ChronoTrigger implements Runnable{
	private ConcurrentLinkedQueue<Ticked> tickList;
	private boolean go;
	
	public ChronoTrigger(){
		go = true;
		(new Thread(this)).start();
	}
	
	public boolean addTicked(Ticked t)
	{
		//adds new Ticked to beginning of the tickList
		tickList.add(t);
		return(true);
	}
	
	public boolean removeTicked(Ticked t)
	{
		return(tickList.remove(t));
	}

	public void stop()
	{
		go = false;
	}
	
	@Override
	public void run() {
		Iterator<Ticked> tickedIterator;
		while(go)
		{
			//cycles through all Tickeds in tickList every 0.1 seconds until stop is called
			try{
				Thread.sleep(100);
			}catch(InterruptedException e)
			{
				
			}
			for(tickedIterator = tickList.iterator(); tickedIterator.hasNext();)
			{
				tickedIterator.next().tick();
			}
		}
	}
}
